package presentation.data;

import java.util.List;
import java.util.logging.Logger;

import presentation.gui.PaymentWindow;
import presentation.gui.ShippingBillingWindow;
import usecasecontrol.CheckoutController;
import business.SessionCache;
import business.customersubsystem.CustomerSubsystemFacade;
import business.exceptions.BackendException;
import business.exceptions.ParseException;
import business.externalinterfaces.Address;
import business.externalinterfaces.CreditCard;
import business.externalinterfaces.CustomerConstants;
import business.externalinterfaces.CustomerProfile;
import business.externalinterfaces.CustomerSubsystem;
import business.util.CustomerUtil;
import business.util.StringParse;

public class CheckoutData {
	private static CheckoutData INSTANCE = new CheckoutData();
	@SuppressWarnings("unused")
	private CheckoutController usecaseControl = new CheckoutController();

	public static CheckoutData getInstance() {
		return INSTANCE;
	}

	private SessionCache sessionContext = SessionCache.getInstance();
	@SuppressWarnings("unused")
	private static final Logger LOG = Logger.getLogger(CheckoutData.class
			.getPackage().getName());
	private static final String TERMS_MESSAGE_FILE = CustomerConstants.CURR_DIR
			+ "\\resources\\terms.txt";
	private static final String GOODBYE_FILE = CustomerConstants.CURR_DIR
			+ "\\resources\\goodbye.txt";

	public String extractGoodbyeMessage() throws ParseException {
		return StringParse.extractTextFromFile(GOODBYE_FILE);
	}

	public String extractTermsText() throws ParseException {
		return StringParse.extractTextFromFile(TERMS_MESSAGE_FILE);
	}

	public Address getDefaultShipAddress() {
		CustomerSubsystem cust = (CustomerSubsystem) sessionContext
				.get(CustomerConstants.CUSTOMER);
		return (cust == null) ? null : cust.getDefaultShippingAddress();
	}

	public Address getDefaultBillAddress() {
		CustomerSubsystem cust = 
			(CustomerSubsystem) sessionContext.get(CustomerConstants.CUSTOMER);
		return (cust == null) ? null : cust.getDefaultBillingAddress();
	}
	
	

	public CustomerProfile getCustomerProfile() {
		CustomerSubsystem cust = (CustomerSubsystem) sessionContext
				.get(CustomerConstants.CUSTOMER);
		return (cust == null) ? null : cust.getCustomerProfile();
	}

	public List<String[]> getSavedAddresses() throws BackendException {
		//implement
		return null;
	}
	
	public String getFullName() {
		CustomerSubsystem cust = 
			(CustomerSubsystem) SessionCache.getInstance().get(CustomerConstants.CUSTOMER);
		String fullname = cust.getCustomerProfile().getFirstName() + " "
			+ cust.getCustomerProfile().getLastName();
		return fullname;
	}
	
	public boolean isNewShippingAddress(ShippingBillingWindow w) {
		return w.isNewShipAddress();
	}
	
	public Address getShippingAddressFromWindow(ShippingBillingWindow w) {
		String[] addrFlds = w.getShipAddressFields();
		return CustomerSubsystemFacade.createAddress(addrFlds[0], addrFlds[1],
				addrFlds[2], addrFlds[3]);
	}
	
	/** Returns billing address specified by user, or shipping address if any billing address fields
	 *  are not specified.
	 */
	public Address getBillingAddressFromWindow(ShippingBillingWindow w) {
		String[] addrFlds = w.getBillAddressFields();
		for(String s : addrFlds) {
			if(s.isEmpty())
				return getShippingAddressFromWindow(w);
		}
		return CustomerSubsystemFacade.createAddress(addrFlds[0], addrFlds[1],
				addrFlds[2], addrFlds[3]);
	}
	
	
	public void saveNewAddress(Address addr) throws BackendException {
		CustomerSubsystem cust = 
			(CustomerSubsystem) SessionCache.getInstance().get(CustomerConstants.CUSTOMER);
		cust.saveNewAddress(addr);
	}
	public boolean isLoggedIn() {
		return DataUtil.isLoggedIn();
	}
	
	
}
